package network;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

public class Packet_Request extends Packet {
	public short m_instance;//instance of this request
	public short m_entity;
	public short m_x;
	public short m_y;
	public short m_z;
	public byte m_bitfield;
	
	public Packet_Request(short instance, short entity, short x, short y, short z, InetAddress address, int timestamp)
	{
		m_type=packettype.REQUEST;
		m_timestamp=timestamp;
		m_address=address;
		m_instance=instance; m_entity=entity;
		m_x=x; m_y=y; m_z=z;
		m_bitfield=0b1111;
	}
	
	public Packet_Request(DatagramPacket pack)
	{
		m_address=pack.getAddress();
		ByteBuffer buffer=ByteBuffer.wrap(pack.getData());
		Header(pack.getAddress(),buffer);		
		//read instance
		m_instance=buffer.getShort();
		//read entity
		m_entity=buffer.getShort();
		//read x
		m_x=buffer.getShort();
		//read y
		m_y=buffer.getShort();
		//read z
		m_z=buffer.getShort();
		m_bitfield=buffer.get();
	}
	
	public DatagramPacket Encode(InetAddress address, int port)
	{
		byte []buffer=new byte[1024];
		ByteBuffer BB=ByteBuffer.wrap(buffer);
		//encode type
		BB.put(getTypebyte(m_type));
		BB.putInt(m_timestamp);		
		//write instance, entity, x, y, z
		BB.putShort(m_instance);
		BB.putShort(m_entity);
		BB.putShort(m_x);
		BB.putShort(m_y);
		BB.putShort(m_z);
		BB.put(m_bitfield);
		return new DatagramPacket(buffer,buffer.length,address,port);	
		
	}
}
